const app = getApp();
Page({
  data: {
    score:0,
    questionList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    //let questionList = JSON.parse(options.data);
    let questionList = app.globalData.userQuetionList;

    var rightCount = 0;
    //遍历问题，设置正确答案
    questionList = questionList.map(function (item) {
      if (item.right == item.user_select)
     {
        rightCount++;
     }
     if (item.right =="A")
     {
       item.right_answer = item.a;
     } else if (item.right =="B")
     {
       item.right_answer = item.b;
     } else if (item.right =="C") {
       item.right_answer = item.c;
     } else if (item.right =="D") {
       item.right_answer = item.d;
     }

     //如果答错了，则记录到错题本
     if (item.right != item.user_select)
     {
       that.recordErrorQuestion(item);
     }
     
     return item;
    });

    //设置成绩
    this.setData({
      questionList: questionList,
      score: 100 * rightCount/questionList.length
    });
  },


  //记录封装的错题
  recordErrorQuestion: function (question) {
    try {
      let myQuestion = wx.getStorageSync('my_question');
      if (!myQuestion) {
        myQuestion = new Array();
      }
      myQuestion.push(question);
      wx.setStorageSync('my_question', myQuestion);

    } catch (e) {

    }
  },

  //查询试题详情
  showQuestionDetail: function (e) {
    let idx = e.currentTarget.dataset.idx;
    let question = this.data.questionList[idx];
    let data = JSON.stringify(question);

    wx.navigateTo({
      url: '/pages/tools/result/question-detail?data=' + data,
    })
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})